package homework.test8;

public class MyThread8_2 implements Runnable{
    private int ticket = 100;

    @Override
    public void run() {
        while (true){
            if("窗口一".equals(Thread.currentThread().getName())){
                boolean flag = synchronizedMethod();
                if(flag){
                    break;
                }
            }
            if("窗口二".equals(Thread.currentThread().getName())){
                synchronized (this) {
                    if(ticket == 0){
                        break;
                    }else {
                        ticket--;
                        System.out.println(Thread.currentThread().getName()+"在卖票，还剩"+ticket+"张票");
                    }
                }
            }
        }
    }

    private boolean synchronizedMethod() {
        if(ticket == 0){
            return true;
        }else {
            ticket--;
            System.out.println(Thread.currentThread().getName()+"在卖票，还剩"+ticket+"张票");
        }
        return false;
    }
}
